<!--
 * @author hemengxia
 游戏管理
 -->
<template>
  <div id="gamemanage">
    <el-collapse-transition>
      <div v-show="searchshow" class="course-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="searchQuery.name"
          placeholder="请输入游戏名称搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <div class="course-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
        </div>
      </div>
    </el-collapse-transition>
    <div class="btn-container">
      <div class="btn-container-title">
        <span>游戏管理</span>
      </div>
      <div class="btn-container-btns">
        <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate">新建</el-button>
        <!--        <el-button class="filter-item" style="margin-left: 10px;" @click="handleDeletes">批量删除</el-button>-->
      </div>
    </div>
    <section class="main-table">
      <v-table :list="list" tag="gameManage" :list-loading="listLoading" :filter-column="filtercolumn">
        <el-table-column label="操作" fixed="right" align="center" min-width="100" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="handlePublish(scope.row)"
            >{{ scope.row.gameStatus === 0 ? "禁用" : "启用" }}
            </el-button>
            <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </v-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </section>
    <el-dialog
      v-el-drag-dialog
      :close-on-click-modal="false"
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      center
      width="800px"
    >
      <el-form
        ref="addForm"
        v-loading="submitloading"
        :model="addForm"
        :rules="addRules"
        class="dataForm"
        label-position="right"
        label-width="120px"
        style="height:410px"
      >
        <el-form-item label="游戏名称" prop="name">
          <el-input v-model.trim="addForm.name" placeholder="请输入游戏名称" />
        </el-form-item>
        <el-form-item label="游戏路径" prop="url">
          <el-input v-model.trim="addForm.url" placeholder="请输入游戏路径" />
        </el-form-item>
        <el-form-item label="游戏类型" prop="type">
          <el-select
            v-model.trim="addForm.type"
            style="width: 100%"
            placeholder="请选择"
          >
            <el-option
              v-for="item in dictList"
              :key="item.id"
              :label="item.dictName"
              :value="item.dictValue"
              :title="item.dictName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="参数1" prop="param1">
          <el-input v-model.trim="addForm.param1" placeholder="请输入参数1" />
        </el-form-item>
        <el-form-item label="参数2" prop="param2">
          <el-input v-model.trim="addForm.param2" placeholder="请输入参数2" />
        </el-form-item>
        <el-form-item label="参数3" prop="param3">
          <el-input v-model.trim="addForm.param3" placeholder="请输入参数3" />
        </el-form-item>
        <el-form-item label="参数4" prop="param3">
          <el-input
            v-model="addForm.param4"
            oninput="value=value.replace(/[^0-9.]/g,'')"
            placeholder="请输入参数4"
          />
        </el-form-item>
        <el-form-item label="参数5" prop="param5">
          <el-date-picker
            v-model="addForm.param5"
            type="datetime"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
          />
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button style="padding: 10px 30px;" @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryPage,
  add,
  get,
  update,
  deleteById,
  getDic,
  updateState
} from '@/views/gamemanage/api/index'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import permission from '@/directive/permission/index.js' // 权限判断指令
// eslint-disable-next-line no-unused-vars
import elDragDialog from '@/directive/el-drag-dialog'
import VTable from '@/components/Table'

export default {
  name: 'GameManage',
  components: {
    Pagination,
    VTable
  },
  directives: {
    permission,
    elDragDialog
  },
  data() {
    return {
      submitloading: false,

      listQuery: {
        page: 1,
        pageSize: 10
      },
      addForm: {
        name: '',
        type: '',
        url: '',
        param1: '',
        param2: '',
        param3: '',
        param4: '',
        param5: '',
        gameStatus: 1

      },
      searchshow: true,
      searchQuery: {
        name: ''
      },

      list: [],
      total: 0,
      dialogStatus: '',
      dialogFormVisible: false,
      textMap: {
        create: '新建',
        update: '编辑'
      },
      multipleSelection: [],
      dictList: [],
      listLoading: false,
      addRules: {
        name: [{
          required: true,
          message: '请填写游戏名称',
          trigger: 'blur'
        }],
        url: [{
          required: true,
          message: '请填写游戏路径',
          trigger: 'blur'
        }],
        type: [{
          required: true,
          message: '请选择游戏类型',
          trigger: 'change'
        }]
      }
    }
  },

  created() {
    this.getDicList()
    this.getList()
  },
  methods: {
    /**
     * 启用/禁用
     */
    handlePublish(row) {
      this.$confirm(row.gameStatus === 0 ? '确认禁用？' : '确认启用？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateState({ id: row.id }).then(res => {
          if (res.data.code === '200') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getList()
          }
        })
      }).catch(() => {

      })
    },
    filtercolumn(scope, item) {
      return scope.row[item]
    },
    getDicList() {
      const obj = {
        dictCode: 'GAMETYPE'
      }
      getDic(obj).then(res => {
        this.dictList = (res.data.data && res.data.data.length > 0) ? res.data.data[0].list : []
      })
    },

    getList() {
      Object.assign(this.searchQuery, this.listQuery)
      this.listLoading = true
      return new Promise((resolve, reject) => {
        queryPage(this.searchQuery).then(res => {
          this.listLoading = false
          this.list = res.data.data.rows || []
          this.total = res.data.data.total
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resettingBtn() {
      this.listQuery.page = 1
      this.listQuery.pageSize = 10
      for (const key in this.searchQuery) {
        this.searchQuery[key] = ''
      }
      this.getList()
    },

    handleCreate() {
      this.dialogStatus = 'create'
      for (const key in this.addForm) {
        if (key === 'gameStatus') {
          this.addForm[key] = 1
        } else {
          this.addForm[key] = ''
        }
      }
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        get({
          id: row.id
        }).then(res => {
          loading.close()
          const data = res.data.data
          for (const key in this.addForm) {
            this.addForm[key] = data[key]
          }
          this.addForm.id = data.id
          this.dialogStatus = 'update'
          this.dialogFormVisible = true
          this.$nextTick(() => {
            this.$refs['addForm'].clearValidate()
          })
        }).catch(error => {
          loading.close()
          reject(error)
        })
      })
    },
    handleDeletes() {
      const arr = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach(item => {
          arr.push(item.id)
        })
        const ids = arr.join(',')
        this.del(ids)
      } else {
        this.$message({
          message: '请选择需要删除的数据！',
          type: 'warning'
        })
      }
    },
    handleDelete(row) {
      this.del(row.id)
    },
    del(ids) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteById({
            id: ids
          }).then(res => {
            this.$message.success('删除成功!')
            this.getList()
          })
        })
        .catch(() => {
        })
    },
    createData() {
      this.$refs['addForm'].validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          add(this.addForm).then(res => {
            loading.close()
            this.$message.success('创建成功！')
            this.dialogFormVisible = false
            this.listQuery.page = 1
            this.getList()
          }).catch(() => {
            loading.close()
          })
        }
      })
    },
    updateData() {
      this.$refs['addForm'].validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          update(this.addForm)
            .then(res => {
              loading.close()
              this.$message.success('编辑成功！')
              this.dialogFormVisible = false
              this.listQuery.page = 1
              this.getList()
            })
            .catch(() => {
              loading.close()
            })
        }
      })
    }

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" type="text/scss">
$dark_gray: #bfced6;

#gamemanage {
  padding: 10px 20px 10px 10px;

  .course-search {
    background-color: #fff;
    padding: 0 10px;

    .svg-container {
      padding: 0 5px 0 10px;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;

      &_login {
        font-size: 20px;
      }
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: "";
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .course-search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .course-search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .course-search-inner {
        padding-top: 10px;

        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }

          .el-input {
            position: relative;

            .el-input__inner {
              border: none;
              padding: 0;
            }

            &:after {
              content: "";
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }

      .course-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .btn-container {
    display: flex;
    align-items: center;
    margin-top: 5px;
    margin-bottom: 5px;

    .btn-container-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      color: rgba(51, 51, 51, 1);
    }
  }

  .dataForm {
    width: 100%;

    .el-radio__label {
      display: inline-block;
      max-width: 380px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
  }

  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }

  .el-checkbox__label {
    font-weight: 400;
  }

  .el-form-item__label {
    // width: 90px !important;
  }

  .el-form-item__content {
    // margin-left: 90px !important;
  }
}
</style>
